 //控制层 
app.controller('itemCatController' ,function($scope,$controller   ,itemCatService){	
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		itemCatService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	};
	
	//分页
	$scope.findPage=function(page,rows){			
		itemCatService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	};
	
	//查询实体 
	$scope.findOne=function(id){				
		itemCatService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	};
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=itemCatService.update( $scope.entity ); //修改  
		}else{
            $scope.entity.parentId = $scope.parentId;
			serviceObject=itemCatService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
                    // $scope.reloadList();//重新加载
                    $scope.findItemCatListByParentId($scope.parentId);
				}else{
					alert(response.message);
				}
			}		
		);				
	};
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		itemCatService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					// $scope.reloadList();//刷新列表
                    $scope.findItemCatListByParentId($scope.parentId);
					$scope.selectIds=[];
				}						
			}		
		);				
	};
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		itemCatService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	};

	//find the list of goods  -> 查询商品分类列表
	$scope.findItemCatListByParentId = function (parentId) {
		itemCatService.findItemCatListByParentId(parentId).success(function (response) {
			$scope.catList = response;
        })
    };

//    difination the grade be used of breadcrumbs
	$scope.grade = 1;

	$scope.parentId = 0;

	$scope.setGrade = function (value) {
		$scope.grade = value;
    };

    //定义查询方法，此方法记录每一个级别对象
	$scope.selectList = function (entity) {

        //查询时候，记录此节点的父节点id
        $scope.parentId = entity.id;

        if($scope.grade == 1){
            $scope.entity_1 = null;
            $scope.entity_2 = null;
        }else if($scope.grade == 2){
            $scope.entity_1 = entity;
            $scope.entity_2 = null;
        }else if($scope.grade == 3){
            $scope.entity_2 = entity;
        }
        $scope.findItemCatListByParentId(entity.id);
    }
});	
